local hengzheng = fk.CreateSkill {
  name = "lvshi__hengzheng",
  dynamic_desc = function (self, player)
    local x = 0
    local str = {}
    for i = 1, 3, 1 do
      if player:getMark("ls__hengzheng" .. tostring(i)) > 0 then
        table.insert(str, "<font color='grey'><s>"..Fk:translate("lvshi__hengzheng"..i).."</s></font>")
        x = x + 1
      else
        table.insert(str, Fk:translate("lvshi__hengzheng"..i))
      end
    end
    if player:getMark("hengzhengedit") > 0 then
      table.insert(str, Fk:translate("hengzhengedit"))
    end
    if x == 3 then
      return "dummyskill"
    else
      return "ls__hengzheng_inner:"..table.concat(str, "；")
    end
  end,
}

Fk:loadTranslationTable{
  ["lvshi__hengzheng"] = "横征",
  [":lvshi__hengzheng"] = "结束阶段，根据你的体力值，你可视为对攻击范围内任意名角色使用对应勇策略牌："..
  "不为最大，【杀】；并列最大，【顺手牵羊】；为最大，【火攻】；然后你可删去对应项，再视为对其中手牌数不小于你的角色使用之。",
  [":ls__hengzheng_inner"] = "结束阶段，根据你的体力值，你可视为对攻击范围内任意名角色使用对应勇策略牌：{1}且你可删去对应项，再视为对其中手牌数不小于你的角色使用之。",
  [":dummyskill"] = "无效果。",
  ["ls__hengzheng1"] = "【杀】",
  ["ls__hengzheng2"] = "【顺手牵羊】",
  ["ls__hengzheng3"] = "【火攻】",
  ["#lvshi__hengzheng-target"] = "横征：你可以选择攻击范围内任意名角色",
  ["#lvshi__hengzheng-lose"] = "你可以删去此项，视为对其中手牌数不小于你的角色使用之",
  ["lvshi__hengzheng1"] = "不为最大，【杀】",
  ["lvshi__hengzheng2"] = "并列最大，【顺手牵羊】",
  ["lvshi__hengzheng3"] = "为最大，【火攻】",
  ["hengzhengedit"] = "<font color='#E8251E'>然后你失去1点体力</font>",

  ["$lvshi__hengzheng1"] = "尔欲行大事？问过吾掌中兵刃否？",
  ["$lvshi__hengzheng2"] = "西凉铁骑曳城，天下高楼可摧。",
}

local DIY = require "packages/diy_utility/diy_utility"
local ls = require "packages/lvshi/utility"

hengzheng:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hengzheng.name) and target == player and player.phase == Player.Finish then
      return player:getMark("ls__hengzheng1") == 0 or player:getMark("ls__hengzheng2") == 0 or player:getMark("ls__hengzheng3") == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player), function (p)
      return player:inMyAttackRange(p) end)
      local to = player.room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = #targets,
        targets = targets,
        skill_name = hengzheng.name,
        prompt = "#lvshi__hengzheng-target",
        cancelable = true,
      })
      if #to > 0 then
        event:setCostData(self, {tos = to})
        return true
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    local patterns = {"slash", "lvshi__snatch", "lvshi__fire_attack"}
    local n = 1
    if table.every(room:getOtherPlayers(player), function (p) return p.hp <= player.hp end) then
      n = 2
    end
    if table.every(room:getOtherPlayers(player), function (p) return p.hp < player.hp end) then
      n = 3
    end
    if player:getMark("ls__hengzheng" .. n) > 0 then return end
    local targets = table.filter(tos, function (p) return player:inMyAttackRange(p) end)
    room:useVirtualCard(patterns[n], nil, player, targets, hengzheng.name)
    if player:isAlive() and room:askForSkillInvoke(player, hengzheng.name, nil, "#lvshi__hengzheng-lose") then
      targets = table.filter(targets, function (p) return p:getHandcardNum() >= player:getHandcardNum() end)
      room:addPlayerMark(player, "ls__hengzheng" .. n)
      if #targets > 0 then
        room:useVirtualCard(patterns[n], nil, player, targets, hengzheng.name)
      end
    end
  end,
})


hengzheng:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(hengzheng.name) and target == player and data.card.skillName == hengzheng.name
    and data.card.type == Card.TypeTrick
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.strategy = data.card.trueName.."_courage"
  end,
})

return hengzheng